<?php

namespace common\logics;

use Yii;
use common\helpers\ArrayHelper;
use common\helpers\Url;

/**
 * 全局链接自由跳转模型
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class NativeLinkLogic extends \yii\base\Model {

    /**
     * @var integer 比赛ID
     */
    public $url;

    /**
     * @var integer 赛事ID
     */
    public $type;

    /**
     * @var integer 比赛状态
     */
    public $name;

    /**
     * @var array 主队信息
     */
    private $_link = [];

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['id', 'competition_id', 'status', 'match_time', 'begin_time'], 'integer'],
            [['teams', 'events', 'season'], 'safe'],
            [['home', 'away'], 'valiteMatchTeam'],
        ];
    }

    /**
     * 获取单个球队信息
     * @inheritdoc
     */
    public function valiteMatchTeam($attribute) {
        $team = $this->$attribute;
        if (empty($team)) {
            return;
        }
        $model = new NanoAnalysisTeamData();
        $model->teams = $this->teams;
        $model->load($team, '');
        if ($model->validate()) {
            $param = '_' . $attribute;
            $this->$param = $model;
        }
    }

    /**
     * 获取单个球队信息
     * @inheritdoc
     */
    public function getEvent() {
        return [
            'name' => ArrayHelper::getValue($this->events, $this->competition_id . '.name_zh'),
            'year' => ArrayHelper::getValue($this->season, 'year'),
        ];
    }

    /**
     * 获取单个球员信息
     * @inheritdoc
     */
    public function getHomeData() {
        return $this->_home;
    }

    /**
     * 获取单个球员信息
     * @inheritdoc
     */
    public function getAwayData() {
        return $this->_away;
    }

    /**
     * 获取单个球员信息
     * @inheritdoc
     */
    public function getResult() {
        $currentHomeid = $this->current_home_id;

        $homeid = $this->_home->id;
        if (!$currentHomeid) {
            $currentHomeid = $homeid;
        }
        $home = ArrayHelper::getValue($this->_home, 'score', 0);
        $away = ArrayHelper::getValue($this->_away, 'score', 0);
        if ($currentHomeid != $homeid) {
            $temp = $home;
            $home = $away;
            $away = $temp;
        }

        if ($home > $away) {
            return [
                'type' => 'danger',
                'text' => '赢'
            ];
        } elseif ($home == $away) {
            return [
                'type' => 'success',
                'text' => '平'
            ];
        }
        return [
            'type' => 'primary',
            'text' => '输'
        ];
    }

    public function getCombineKeys() {
        return [
            'id',
            'competition_id',
            'status',
            'match_time',
            'begin_time',
            'home' => [
                'id',
                'ranking',
                'score',
                'half_score',
                'red',
                'yellow',
                'corner',
                'addtime',
                'penalty',
            ],
            'away' => [
                'id',
                'ranking',
                'score',
                'half_score',
                'red',
                'yellow',
                'corner',
                'addtime',
                'penalty',
            ],
            false,
            false,
            'season' => [
                'id',
                'year',
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function fields() {
        return [
            'id',
            'event',
            'status',
            'match_date' => function() {
                return date('Y-m-d', $this->match_time);
            },
            'home' => 'homeData',
            'away' => 'awayData',
            'result',
        ];
    }

}
